package com.axc.gallery2.edit.overLay;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.axc.gallery2.R;
import com.axc.gallery2.album.AlbumListViewActivity;
import com.axc.gallery2.edit.SaveBitmap;
import com.axc.gallery2.edit.activity.MenuIcon;

public class EditOverLay extends RelativeLayout {

	/**
	 * 概要： 拖放一次图片常见一个对象 上下菜单 上菜单用来显示要拖进来的图片，或者文字 下菜单用来显示基本菜单， 暂时不要==确定（确定图片放的位置）
	 * 确定 取消 保存 重设 说明 返回
	 */
	// 头部
	LinearLayout headView;
	HorizontalScrollView headscroll;
	// 中间
	LinearLayout middleView;

	// 底部
	LinearLayout bottomView;
	HorizontalScrollView bottomscroll;

	OverLayView view;

	Context context;

	OverLayAdapter adapter = new OverLayAdapter();

	int shapeTyes = 0;
	int penstyle = 0;
	int earsesize = 0;

	DecoratePics decoratePics = new DecoratePics();

	public EditOverLay(Context context, int w, int h) {
		super(context);
		// this.context=context;
		/*
		 * Thread thread=new Thread(new TCreateMiddle(context,w, h));
		 * thread.start(); thread.setPriority(Thread.MAX_PRIORITY); Thread
		 * thread2=new Thread(new TCreateHead()); thread2.start();
		 * 
		 * Thread thread3 =new Thread(new TCreateBottom()); thread3.start();
		 */
		TCreateMiddle t2 = new TCreateMiddle(context, w, h);
		t2.run();

		TCreateHead t1 = new TCreateHead();
		t1.run();

		TCreateBottom t3 = new TCreateBottom();
		t3.run();
	}

	class TCreateHead implements Runnable {

		@Override
		public void run() {
			headscroll = new HorizontalScrollView(context);
			headscroll.setBackgroundColor(R.color.transparent);
			headView = new LinearLayout(context);
			RelativeLayout.LayoutParams headParams = new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.FILL_PARENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			headParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

			Add(decoratePics.getImg());

			// 为了以后的宽展，所以这里引入了 HorizontalScrollView 的滚动形式
			headscroll.addView(headView, headParams);
			EditOverLay.this.addView(headscroll, headParams);
		}

		public void Add(int[][] rs) {
			for (int i = 0; i < rs.length; i++) {
				decoratePics.AddPic(headView, adapter, view, context,
						R.string.pic3, rs[i][0], rs[i][1], rs[i][2]);
			}
		}
	}

	class TCreateMiddle implements Runnable {
		int w, h = 0;

		public TCreateMiddle(Context context, int w, int h) {
			/*
			 * w=EditOverLay.this.w; h=EditOverLay.this.h;
			 */
			EditOverLay.this.context = context;
			this.w = w;
			this.h = h;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Log.w("menu", "middle");
			view = new OverLayView(context, w, h);
			middleView = new LinearLayout(context);
			RelativeLayout.LayoutParams middleParams = new RelativeLayout.LayoutParams(
					w, h);
			middleParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
			middleParams.addRule(RelativeLayout.CENTER_VERTICAL);

			middleView.addView(view);
			EditOverLay.this.addView(middleView, middleParams);
		}

	}

	class TCreateBottom implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Log.w("menu", "bottom");
			bottomView = new LinearLayout(context);
			bottomView.setBackgroundColor(R.color.transparent);
			RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			bottomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

			LinearLayout btnSure = new MenuIcon(context, R.string.sure,
					R.drawable.ensure_icon);
			btnSure.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					adapter.enSure(view);
				}
			});
			bottomView.addView(btnSure);

			LinearLayout btnCancle = new MenuIcon(context, R.string.cancel,
					R.drawable.cancel_icon);
			btnCancle.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					adapter.setCancel(view);
				}
			});

			bottomView.addView(btnCancle);

			LinearLayout saveLayout = new MenuIcon(context, R.string.save,
					R.drawable.save_icon);
			saveLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					new SaveBitmap().Save(context, OverLayView.mBitmapSave);
				}
			});
			bottomView.addView(saveLayout);

			// 橡皮擦
			LinearLayout earseLayout = new MenuIcon(context, R.string.aearser,
					R.drawable.earse_icon);
			earseLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					adapter.bgEraser(view, 20);
				}
			});
			bottomView.addView(earseLayout);

			// 橡皮擦大小
			LinearLayout earseSize = new MenuIcon(context, R.string.asize,
					R.drawable.zoom_icon);
			earseSize.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Dialog dialog = new AlertDialog.Builder(context).setTitle(
							"橡皮擦").setSingleChoiceItems(
							new String[] { "x1", "x3", "x5", "x7", "x9" },
							EditOverLay.this.earsesize,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									switch (which) {
									case 0:
										adapter.setEarseSize(view, 20);
										earsesize = 0;
										break;
									case 1:
										adapter.setEarseSize(view, 40);
										earsesize = 1;
										break;
									case 2:
										adapter.setEarseSize(view, 60);
										earsesize = 2;
										break;
									case 3:
										adapter.setEarseSize(view, 80);
										earsesize = 3;
										break;
									case 4:
										adapter.setEarseSize(view, 100);
										earsesize = 4;
										break;
									}
								}
							}).setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dialog.dismiss();
								}
							}).create();
					dialog.show();
				}
			});
			bottomView.addView(earseSize);

			// 返回
			LinearLayout outLayout = new MenuIcon(context,
					R.string.render_dissmiss, R.drawable.back_activity);
			outLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					adapter.BackToPreActivity(OverLayActivity.over);
				}
			});
			bottomView.addView(outLayout);

			LinearLayout homeLayout = new MenuIcon(context, R.string.homepage,
					R.drawable.home_activity);
			homeLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(OverLayActivity.over,
							AlbumListViewActivity.class);
					OverLayActivity.over.startActivity(intent);
				}
			});
			bottomView.addView(homeLayout);

			EditOverLay.this.addView(bottomView, bottomParams);
		}
	}
}
